package actionHandlers;

import java.awt.Point;

import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import cuShapes.Arrow;
import cuShapes.Box;
import cuShapes.Ellipse;
import cuShapes.PQShape;

import palette.CanvasManager;
import palette.CanvasWindow;


public class PropertyModelListener implements TableModelListener {

	@Override
	public void tableChanged(TableModelEvent arg0) {
		PQShape currShape = ((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCurrentShape();
		JTable table = currShape.getShapeTable().getCurrentTable();
		JTree tree = ((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getShapeTree().getTree();
		Point adjusted;
		
		// check to see what kind of shape so that we know what the table model looks like
		if(currShape instanceof Arrow) {
			switch(arg0.getFirstRow()) {
			case 0:
				//System.out.println("Name was changed.");
				currShape.setName((String) table.getModel().getValueAt(0, 1));
				((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().repaint();			
				TreePath selPath = tree.getSelectionPath();
				((DefaultMutableTreeNode) (selPath.getLastPathComponent())).setUserObject((PQShape) currShape);
				tree.updateUI();
				break;
			/**	
			 * The next cases were commented out because I could not find a good use for letting the user editing 
			 *	the X,Y,length of an Arrow. 
			 **	
			case 1:
				System.out.println("X was changed.");
				currShape.setWorkingRect(currShape.getCenterRect());
				adjusted = toCenter(new Point((int) Float.parseFloat((String) table.getModel().getValueAt(1, 1)),(int) Float.parseFloat((String) table.getModel().getValueAt(2, 1))), currShape);
				currShape.moveWorkingRect(adjusted);
				((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().repaint();			
				break;
			case 2:
				System.out.println("Y was changed.");
				currShape.setWorkingRect(currShape.getCenterRect());
				currShape.moveWorkingRect((Point) new Point((int) Float.parseFloat((String) table.getModel().getValueAt(1, 1)),(int) Float.parseFloat((String) table.getModel().getValueAt(2, 1))));
				((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().repaint();				
				break;
			case 3:
				System.out.println("Length was changed.");
				currShape.moveWorkingRect((Point) new Point((int) Float.parseFloat((String) table.getModel().getValueAt(1, 1)),(int) Float.parseFloat((String) table.getModel().getValueAt(2, 1))));
				((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().repaint();			
				break;*/
			case 1:
				//System.out.println("Color was changed.");
				//currShape.setName((String) table.getModel().getValueAt(1, 1));
				((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().repaint();			
				break;
			default:
				break;
			}
		} else if(currShape instanceof Box || currShape instanceof Ellipse) {
			switch(arg0.getFirstRow()) {
			case 0:
				//System.out.println("Name was changed.");
				currShape.setName((String) table.getModel().getValueAt(0, 1));
				((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().repaint();
				TreePath selPath = tree.getSelectionPath();
				((DefaultMutableTreeNode) (selPath.getLastPathComponent())).setUserObject((PQShape) currShape);
				tree.updateUI();
				break;
			case 1:
				//System.out.println("X was changed.");
				currShape.setWorkingRect(currShape.getCenterRect());
				adjusted = toCenter(new Point((int) Float.parseFloat((String) table.getModel().getValueAt(1, 1)),(int) Float.parseFloat((String) table.getModel().getValueAt(2, 1))), currShape);
				currShape.moveWorkingRect(adjusted);
				((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().repaint();
				currShape.getShapeTable().removeTableModelListener();
    			currShape.getShapeTable().setProperties(currShape);
    			currShape.getShapeTable().addTableModelListener();
				break;
			case 2:
				//System.out.println("Y was changed.");
				currShape.setWorkingRect(currShape.getCenterRect());
				adjusted = toCenter(new Point((int) Float.parseFloat((String) table.getModel().getValueAt(1, 1)),(int) Float.parseFloat((String) table.getModel().getValueAt(2, 1))), currShape);
				currShape.moveWorkingRect(adjusted);
				((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().repaint();
				currShape.getShapeTable().removeTableModelListener();
    			currShape.getShapeTable().setProperties(currShape);
    			currShape.getShapeTable().addTableModelListener();
				break;
			case 3:
				//System.out.println("Width was changed.");
				currShape.setWorkingRect(currShape.getResizeRect());
				adjusted = toResize(new Point((int) Float.parseFloat((String) table.getModel().getValueAt(3, 1)),(int) Float.parseFloat((String) table.getModel().getValueAt(4, 1))), currShape);
				currShape.moveWorkingRect(adjusted);
				((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().repaint();
				currShape.getShapeTable().removeTableModelListener();
    			currShape.getShapeTable().setProperties(currShape);
    			currShape.getShapeTable().addTableModelListener();
				
				break;
			case 4:
				//System.out.println("Height was changed.");
				currShape.setWorkingRect(currShape.getResizeRect());
				adjusted = toResize(new Point((int) Float.parseFloat((String) table.getModel().getValueAt(3, 1)),(int) Float.parseFloat((String) table.getModel().getValueAt(4, 1))), currShape);
				currShape.moveWorkingRect(adjusted);
				((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().repaint();
				currShape.getShapeTable().removeTableModelListener();
    			currShape.getShapeTable().setProperties(currShape);
    			currShape.getShapeTable().addTableModelListener();
				break;
			case 5:
				//System.out.println("Color was changed.");
				//currShape.setColor((String) table.getModel().getValueAt(5, 1));
				((CanvasWindow) (CanvasManager.getInst().getSelectedFrame())).getCanvas().repaint();				
				break;
			default:
				break;
			}
		}	
	}

	/**
	 * 
	 * @param p
	 * @param s
	 * @return
	 */
	public Point toCenter(Point p, PQShape s) {
		Point newPoint = new Point(p.x+(s.getWidth()/2), p.y+(s.getHeight()/2));
		return newPoint;
	}
	
	/**
	 * 
	 * @param p
	 * @param s
	 * @return
	 */
	public Point toResize(Point p, PQShape s) {
		Point newPoint = new Point(s.getStart().x+p.x, s.getStart().y+p.y);
		return newPoint;
	}
	
    
}
